/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.BusBindingWrapper;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.MappingServiceDetailInfo;
import han.bifang.common.entity.MappingServiceVersionDetailInfo;
import han.bifang.common.entity.ServiceBusDetailInfo;
import han.bifang.common.entity.ServiceDetailInfo;
import han.bifang.common.entity.SoaBusBinding;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaMappingService;
import han.bifang.common.entity.SoaMappingServiceVersion;
import han.bifang.common.entity.SoaMethodParam;
import han.bifang.common.entity.SoaMethodQos;
import han.bifang.common.entity.SoaMqJms;
import han.bifang.common.entity.SoaNormalJms;
import han.bifang.common.entity.SoaService;
import han.bifang.common.entity.SoaServiceBus;
import han.bifang.common.entity.SoaServiceQos;
import han.bifang.common.entity.SoaServiceVersion;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.entity.SoaVersionQos;
import han.bifang.common.entity.SoaWebServicesBinding;
import han.bifang.common.entity.SoaWebServicesMethod;
import han.bifang.common.entity.SoaWebServicesMethodWrapper;
import han.bifang.common.entity.SoaWsdlWrapper;
import han.bifang.common.entity.VersionDetailInfo;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class QueryDao {

	public static ServiceDetailInfo getServiceDetailByCode(String code)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaService s where s.serviceCode = ? ";
			ServiceDetailInfo ret = null;
			List ses = DaoManager.getDao().find(hql, code);
			if (ses.size() > 0) {

				SoaService ss = (SoaService) ses.get(0);
				ret = new ServiceDetailInfo();
				ret.setService(ss);
				hql = "from SoaServiceQos s where s.serviceId = ?";
				List<SoaServiceQos> qos = DaoManager.getDao().find(hql,
						ss.getId());
				ret.setQos(qos);
				hql = "select s.compId.busId from SoaBusServiceRel s where s.compId.serviceId = ?";
				List<String> busRel = DaoManager.getDao().find(hql, ss.getId());
				ret.setBusRel(busRel);

				hql = "from SoaServiceVersion s where s.serviceId = ? ";
				List<SoaServiceVersion> l = DaoManager.getDao().find(hql,
						ss.getId());
				ArrayList<VersionDetailInfo> versions = new ArrayList<VersionDetailInfo>();
				for (SoaServiceVersion v : l) {
					VersionDetailInfo vd = new VersionDetailInfo();
					vd.setVersion(v);
					hql = "from SoaVersionQos s where s.versionId = ?";
					List<SoaVersionQos> vqos = DaoManager.getDao().find(hql,
							v.getId());
					vd.setQos(vqos);
					if (v.getBindingType().equals(EntityConstants.BINDING_HTTP)) {
						SoaHttpBinding o = (SoaHttpBinding) DaoManager.getDao()
								.get(SoaHttpBinding.class, v.getBindingId());
						vd.setBinding(o);

					} else if (v.getBindingType().equals(
							EntityConstants.BINDING_SOAP)) {
						SoaSoapBinding o = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, v.getBindingId());
						vd.setBinding(o);

					} else if (v.getBindingType().equals(
							EntityConstants.BINDING_JMS)) {
						SoaJmsBindingWrapper w = new SoaJmsBindingWrapper();
						SoaJmsBinding o = (SoaJmsBinding) DaoManager.getDao()
								.get(SoaJmsBinding.class, v.getBindingId());
						w.setJmsBinding(o);
						if (o.getServer().equals("mq")) {
							SoaMqJms j = (SoaMqJms) DaoManager.getDao().get(
									SoaMqJms.class, o.getInfoId());
							w.setInfo(j);
						} else {
							SoaNormalJms j = (SoaNormalJms) DaoManager.getDao()
									.get(SoaNormalJms.class, o.getInfoId());
							w.setInfo(j);
						}
						vd.setBinding(w);

					} else if (v.getBindingType().equals(
							EntityConstants.BINDING_WEB_SERVICES)) {
						SoaWsdlWrapper w = new SoaWsdlWrapper();
						SoaWebServicesBinding o = (SoaWebServicesBinding) DaoManager
								.getDao().get(SoaWebServicesBinding.class,
										v.getBindingId());
						w.setWebServicesBinding(o);

						hql = "from SoaWebServicesMethod s where s.versionId = ?";
						List<SoaWebServicesMethod> wml = DaoManager.getDao()
								.find(hql, v.getId());
						ArrayList<SoaWebServicesMethodWrapper> wmwl = new ArrayList<SoaWebServicesMethodWrapper>();
						for (SoaWebServicesMethod wm : wml) {
							SoaWebServicesMethodWrapper wmw = new SoaWebServicesMethodWrapper();
							wmw.setMethod(wm);
							hql = "from SoaMethodQos s where s.methodId = ?";
							List<SoaMethodQos> mqos = DaoManager.getDao().find(
									hql, wm.getId());
							wmw.setQos(mqos);
							wmwl.add(wmw);
							hql = "from SoaMethodParam s where s.serviceMethodId = ?";
							List<SoaMethodParam> tmp = DaoManager.getDao()
									.find(hql, wm.getId());
							wmw.setParams(tmp.get(0));

						}
						w.setMethods(wmwl);
						vd.setBinding(w);

					}
					versions.add(vd);
				}
				ret.setVersions(versions);
			}

			tx.commit();

			return ret;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceDao.class);
			logger.error("QueryDao.getServiceDetailByCode()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceDetailByCode() error", e);
		} finally {
			tx.end();
		}

	}

	public static MappingServiceDetailInfo getMappingServiceDetailByCode(
			String code) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaMappingService s where s.localCode = ? ";
			MappingServiceDetailInfo ret = null;
			List ses = DaoManager.getDao().find(hql, code);
			if (ses.size() > 0) {
				SoaMappingService s = (SoaMappingService) ses.get(0);
				ret = new MappingServiceDetailInfo();
				ret.setService(s);
				hql = "from SoaServiceQos s where s.serviceId = ?";
				List<SoaServiceQos> qos = DaoManager.getDao().find(hql,
						s.getId());
				ret.setQos(qos);
				hql = "select s.compId.busId from SoaBusServiceRel s where s.compId.serviceId = ?";
				List<String> busRel = DaoManager.getDao().find(hql, s.getId());
				ret.setBusRel(busRel);

				hql = "from SoaMappingServiceVersion s where s.serviceId = ?";
				List<SoaMappingServiceVersion> vl = DaoManager.getDao().find(
						hql, s.getId());
				ArrayList<MappingServiceVersionDetailInfo> vdl = new ArrayList<MappingServiceVersionDetailInfo>();
				for (SoaMappingServiceVersion v : vl) {
					MappingServiceVersionDetailInfo vd = new MappingServiceVersionDetailInfo();
					vd.setVersion(v);
					hql = "from SoaVersionQos s where s.versionId = ?";
					List<SoaVersionQos> vqos = DaoManager.getDao().find(hql,
							v.getId());
					vd.setQos(vqos);
					vdl.add(vd);
				}
				ret.setVersions(vdl);

			}

			tx.commit();

			return ret;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceDao.class);
			logger.error("QueryDao.getMappingServiceDetailByCode()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getMappingServiceDetailByCode() error", e);
		} finally {
			tx.end();
		}

	}

	public static ServiceBusDetailInfo getServiceBusDetailById(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			ServiceBusDetailInfo ret = null;
			SoaServiceBus bus = (SoaServiceBus) DaoManager.getDao().get(
					SoaServiceBus.class, id);

			if (bus != null) {
				ret = new ServiceBusDetailInfo();
				ret.setBus(bus);

				String hql = "from SoaBusBinding s where s.busId = ?";
				List<SoaBusBinding> bl = DaoManager.getDao().find(hql, id);
				List<BusBindingWrapper> l = new ArrayList<BusBindingWrapper>();
				for (int i = 0, j = bl.size(); i < j; i++) {
					Log logger = LogFactory.getLog(ServiceDao.class);
					
					SoaBusBinding s = (SoaBusBinding) bl.get(i);
					logger.info(s.getType());
					logger.info(s.getBindingId());
					BusBindingWrapper cbw = new BusBindingWrapper();
					cbw.setBusBinding(s);
					if (s.getType().equals(EntityConstants.BINDING_HTTP)) {
						SoaHttpBinding b = (SoaHttpBinding) DaoManager.getDao()
								.get(SoaHttpBinding.class, s.getBindingId());

						cbw.setHttpBinding(b);

					} else if (s.getType().equals(EntityConstants.BINDING_JMS)) {
						SoaJmsBinding b = (SoaJmsBinding) DaoManager.getDao()
								.get(SoaJmsBinding.class, s.getBindingId());
						SoaNormalJms info = (SoaNormalJms) DaoManager.getDao()
								.get(SoaNormalJms.class, b.getInfoId());

						SoaJmsBindingWrapper jms = new SoaJmsBindingWrapper();
						jms.setInfo(info);
						jms.setJmsBinding(b);
						cbw.setJmsBinding(jms);
					} else if (s.getType().equals(EntityConstants.BINDING_SOAP)) {
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						cbw.setSoapBinding(b);
					} else if (s.getType().equals(
							EntityConstants.BINDING_WEB_SERVICES)) {
						
					
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						logger.info(b);
						cbw.setSoapBinding(b);
					}
					l.add(cbw);

				}

				ret.setBindings(l);

				hql = "from SoaService s where s.busId = ?";
				List<SoaService> ll = DaoManager.getDao()
						.find(hql, bus.getId());
				for (SoaService s : ll) {
					if (s.getServiceCode().endsWith("notify")) {
						ret.setNotifyServiceCode(s.getServiceCode());
						ret.setNotifyServiceVersion(s.getDefaultVersion());
					}
				}

			}

			tx.commit();

			return ret;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceDao.class);
			logger.error("QueryDao.getServiceBusDetailById()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceBusDetailById() error", e);
		} finally {
			tx.end();
		}

	}

	public static ServiceBusDetailInfo getServiceBusDetailByCode(String code)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			ServiceBusDetailInfo ret = null;
			String hql = "from SoaServiceBus b where b.code = ?";
			List<SoaServiceBus> sbl = DaoManager.getDao().find(hql, code);

			if (sbl.size() > 0) {
				ret = new ServiceBusDetailInfo();
				ret.setBus(sbl.get(0));
				String id = ret.getBus().getId();

				hql = "from SoaBusBinding s where s.busId = ?";
				List<SoaBusBinding> bl = DaoManager.getDao().find(hql, id);
				List<BusBindingWrapper> l = new ArrayList<BusBindingWrapper>();
				for (int i = 0, j = bl.size(); i < j; i++) {
					Log logger = LogFactory.getLog(ServiceDao.class);
					SoaBusBinding s = (SoaBusBinding) bl.get(i);
					logger.info(s.getType());
					logger.info(s.getBindingId());
					BusBindingWrapper cbw = new BusBindingWrapper();
					cbw.setBusBinding(s);
					if (s.getType().equals(EntityConstants.BINDING_HTTP)) {
						SoaHttpBinding b = (SoaHttpBinding) DaoManager.getDao()
								.get(SoaHttpBinding.class, s.getBindingId());

						cbw.setHttpBinding(b);

					} else if (s.getType().equals(EntityConstants.BINDING_JMS)) {
						SoaJmsBinding b = (SoaJmsBinding) DaoManager.getDao()
								.get(SoaJmsBinding.class, s.getBindingId());
						SoaNormalJms info = (SoaNormalJms) DaoManager.getDao()
								.get(SoaNormalJms.class, b.getInfoId());

						SoaJmsBindingWrapper jms = new SoaJmsBindingWrapper();
						jms.setInfo(info);
						jms.setJmsBinding(b);
						cbw.setJmsBinding(jms);
					} else if (s.getType().equals(EntityConstants.BINDING_SOAP)) {
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						cbw.setSoapBinding(b);
					}else if (s.getType().equals(
							EntityConstants.BINDING_WEB_SERVICES)) {
						
					
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						logger.info(b);
						cbw.setSoapBinding(b);
					}
					l.add(cbw);

				}

				ret.setBindings(l);

				hql = "from SoaService s where s.busId = ?";
				List<SoaService> ll = DaoManager.getDao().find(hql, id);
				for (SoaService s : ll) {
					if (s.getServiceCode().endsWith("notify")) {
						ret.setNotifyServiceCode(s.getServiceCode());
						ret.setNotifyServiceVersion(s.getDefaultVersion());
					}
				}

			}

			tx.commit();

			return ret;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceDao.class);
			logger.error("QueryDao.getServiceBusDetailByCode()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceBusDetailByCode() error", e);
		} finally {
			tx.end();
		}

	}

}
